package com.matrix.maprender;
 
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.tiled.TileAtlas;
import com.badlogic.gdx.graphics.g2d.tiled.TiledLoader;
import com.badlogic.gdx.graphics.g2d.tiled.TiledMap;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.graphics.g2d.tiled.TileMapRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.matrix.helper.Input;
 
public class PlayingWithTME implements ApplicationListener {
	private TileAtlas tileAtlas;
	private TiledMap map;
	private TileMapRenderer tiledMapRenderer;
 
	private OrthographicCamera camera;
	private OrthoCamController cameraController;
	private ChickGameInput chickGameInput;
	private InputMultiplexer inputMultiplexer;
	private Texture texture;
	private Sprite sprite;
	private SpriteBatch sb;
	private Input input;
	private ShapeRenderer shapeRenderer;
//	private Vector2 maxCamPosition = new Vector2(0, 0);
//	private static final int[] layersList = { 0 };
 
	@Override
	public void create() {
		sb = new SpriteBatch();
		inputMultiplexer = new InputMultiplexer();
		FileHandle packfile = Gdx.files.internal("data/");
 
		map = TiledLoader.createMap(Gdx.files.internal("data/bando.tmx"));
 
		tileAtlas = new TileAtlas(map, packfile);
 
		tiledMapRenderer = new TileMapRenderer(map, tileAtlas,
				Gdx.graphics.getWidth() / 3, Gdx.graphics.getHeight() / 3);
 
		camera = new OrthographicCamera(Gdx.graphics.getWidth(),
				Gdx.graphics.getHeight());
		camera.position.set(tiledMapRenderer.getMapWidthUnits() / 2,
				tiledMapRenderer.getMapHeightUnits() / 2, 0);
		
		cameraController = new OrthoCamController(camera);
		inputMultiplexer.addProcessor(cameraController);
		
		chickGameInput = new ChickGameInput(camera);
		inputMultiplexer.addProcessor(chickGameInput);
		
//		maxCamPosition.set(tiledMapRenderer.getMapWidthUnits(),
//				tiledMapRenderer.getMapHeightUnits());
		Gdx.input.setInputProcessor(inputMultiplexer);
		texture = new Texture(Gdx.files.internal("data/blue_space.jpg"));
		sprite = new Sprite(texture);
		sprite.setPosition(0, 0);
		input = new Input();
		shapeRenderer = new ShapeRenderer();
		shapeRenderer.setProjectionMatrix(camera.combined);
	}
 
	@Override
	public void resume() {
	}
 
	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
 
		camera.update();
 
		tiledMapRenderer.getProjectionMatrix().set(camera.combined);
 
		Vector3 tmp = new Vector3();
		tmp.set(0, 0, 0);
		camera.unproject(tmp);
 
//		tiledMapRenderer.render((int) tmp.x, tiledMapRenderer.getMapHeightUnits() - (int) tmp.y,
//				Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), layersList);
		
		tiledMapRenderer.render();
		chickGameInput.render();
		sb.begin();
		sprite.draw(sb);
		sb.setProjectionMatrix(camera.combined);
		sb.end();
		if ( input.pressKey( Keys.D ))
			sprite.setPosition( sprite.getX() + 5, sprite.getY() );
		if ( input.pressKey( Keys.A ))
			sprite.setPosition( sprite.getX() - 5, sprite.getY() );
		if ( input.pressKey( Keys.W ))
			sprite.setPosition( sprite.getX(), sprite.getY() + 5);
		if ( input.pressKey( Keys.S ))
			sprite.setPosition( sprite.getX(), sprite.getY() - 5 );
		shapeRenderer.begin(ShapeType.Line);
		Gdx.gl10.glLineWidth(5f);
		shapeRenderer.line(0, 0, 100, 0);
		shapeRenderer.end();
	}
 
	@Override
	public void resize(int width, int height) {
	}
 
	@Override
	public void pause() {
	}
 
	@Override
	public void dispose() {
	}
}